﻿using System;
using Reactor.Messages.Commands.Core;
using Reactor.Messaging;

namespace Reactor.CoreHost.Handlers
{
    public class TransferServiceCommandHandler : MessageHandlerBase<TransferService>
    {
        #region Overrides of MessageHandlerBase<TransferService>

        public override void OnHandling(TransferService message, IMessageContext messageContext)
        {
            if (message == null) throw new ArgumentNullException("message");

            var core = GetCurrentServiceAsReactorCore();
            if (message.FromCoreName != core.Identifier.Name)
            {
                Log.WarnFormat("Cannot complete handling TransferService command because the donor Core in the command is not this Core instance. Donor Core name in command: {0}", message.FromCoreName);
                return;
            }

            try
            {
                core.TransferService(message);
            }
            catch (Exception e)
            {
                Log.Error("Core instance threw an exception while transfering service.", e);
            }
        }

        #endregion
    }
}
